using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace SanFS.DiscLibrary
{
	public delegate void CmdWrapperOutputLineHandler(CmdWrapper Sender, int LineNumber, string Line);
	
	public class CmdWrapper
	{
		private string	mCommand;
		private bool	mMergeStderr;
		private bool	mDebug = false;
		
		public CmdWrapper(string Command)
		{
			mCommand = Command;
		}
		
		public string []Run()
		{
			Process	p = new Process();
			p.StartInfo.UseShellExecute = false;
			p.StartInfo.RedirectStandardOutput = true;
			p.StartInfo.RedirectStandardError = true;
			p.StartInfo.FileName = mCommand.Split(' ')[0];
			p.StartInfo.Arguments = mCommand.Substring(mCommand.IndexOf(" ")+1);
			if (mDebug)
				Console.WriteLine("CmdWrapper.Run(): Executing '{0}', args '{1}'", p.StartInfo.FileName, p.StartInfo.Arguments);
			p.Start();
			string	output = p.StandardOutput.ReadToEnd();
			string	error = p.StandardError.ReadToEnd();
			
			p.WaitForExit();
			
			if (!MergeStderr)
				return output.Split('\n');
			string	merged = output + error;
			return merged.Split('\n');
		}
		
		public bool MergeStderr 
		{
			get { return mMergeStderr; }
			set { mMergeStderr = value; }
		}
		
		public string Command
		{
			get { return mCommand; }
		}
		
		public bool Debug
		{
			get { return mDebug; }
		}
	}  // END CLASS
}  // END NAMESPACE
